package exp07gui.doll;

import java.util.Collections;
import java.util.Random;
import java.util.Vector;

public class DoubleColorBall {
	
	static final int MAX = 33;
	private static final String[] RANK = "谢谢惠顾 一等奖 二等奖 三等奖 四等奖 五等奖 六等奖"
			.split("\\s+");
	static Vector<Integer> balls = new Vector<>();
	static Integer sBlueBall;
	static Vector<Integer> sRedBalls = new Vector<>();
	static Random random = new Random();
	static {
		for (int i = 1; i <= MAX; i++)
			balls.add(i);
		Collections.shuffle(balls);
		sBlueBall = random.nextInt(16) + 1;
		sRedBalls.addAll(balls.subList(0, 6));
		Collections.sort(sRedBalls);
	}

	Integer blueBall;
	Vector<Integer> redBalls = new Vector<>();

	public DoubleColorBall() {
		Collections.shuffle(balls);
		blueBall = random.nextInt(16) + 1;
		redBalls.addAll(balls.subList(0, 6));
		Collections.sort(redBalls);
	}

	@Override
	public String toString() {
		StringBuffer buffer = new StringBuffer("");

		for (Integer i : redBalls)
			if (i < 10)
				buffer.append(" (0" + i + ") ");
			else
				buffer.append(" (" + i + ") ");
		if (blueBall < 10)
			buffer.append("[0" + blueBall + "]");
		else
			buffer.append("[" + blueBall + "]");
		return buffer.toString();
	}

	public static String getTopBouns() {
		StringBuffer buffer = new StringBuffer("");

		for (Integer i : sRedBalls)
			if (i < 10)
				buffer.append(" (0" + i + ") ");
			else
				buffer.append(" (" + i + ") ");
		if (sBlueBall < 10)
			buffer.append("[0" + sBlueBall + "]");
		else
			buffer.append("[" + sBlueBall + "]");
		return buffer.toString();
	}

	/**
	 * @param args
	 */
	private static final int total = 100000;
	public static void main(String[] args) {
		System.out.println("双色球(6+1)~");
		System.out.println(getTopBouns());
		System.out.println("--------------------------------");
		int f[] = new int[7];
		for (int i = 0; i < total; i++) {
			DoubleColorBall my = new DoubleColorBall();
			int rank = my.judge();
			f[rank]++;
			if (rank >= 1 && rank <= 2)
				System.out.println(my + RANK[rank]);
		}
		for (int i = 1; i <= 7; i++) {
			int j = (i) % 7;
			System.out.println(RANK[j] + ":" + f[j] + "次");
		}
		System.out.println("总购买:" + total + "次");
		double m[]={0,5e6,2e6,3000,200,10,5};
		double ret=0;
		for(int i=0;i<m.length;i++)
			ret+=m[i]*f[i];
		System.out.println("返奖率："+ret*100/2.0/total+"%");
	}

	private int judge() {
		if (check(6, 1))
			return 1;
		else if (check(6, 0))
			return 2;
		else if (check(5, 1))
			return 3;
		else if (check(5, 0) || check(4, 1))
			return 4;
		else if (check(4, 0) || check(3, 1))
			return 5;
		else if (check(2, 1) || check(1, 1) || check(0, 1))
			return 6;
		else
			return 0;

	}

	private boolean check(int rr, int bb) {
		int red = 0, blue = 0;
		if (blueBall.equals(sBlueBall))
			blue++;
		for (int i = 0; i < redBalls.size(); i++)
			if (redBalls.get(i).equals(sRedBalls.get(i)))
				red++;
		return red == rr && blue == bb;
	}

}
